package org.miyuki4j.cfgtool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/25/星期五 15:32
 * @Version 1.0
 */
public class StringUtils {

    private static final Map<String, String> rawDelimiter2Regex = new HashMap<>();

    static {

        rawDelimiter2Regex.put(".", "\\.");
        rawDelimiter2Regex.put(",", "\\,");
        rawDelimiter2Regex.put("|", "\\|");
        rawDelimiter2Regex.put(":", "\\:");
        rawDelimiter2Regex.put("\\", "\\\\");
        rawDelimiter2Regex.put("[", "\\[");
        rawDelimiter2Regex.put("]", "\\]");
        rawDelimiter2Regex.put("{", "\\{");
        rawDelimiter2Regex.put("}", "\\}");
        rawDelimiter2Regex.put("(", "\\(");
        rawDelimiter2Regex.put(")", "\\)");
        rawDelimiter2Regex.put("*", "\\*");
        rawDelimiter2Regex.put("+", "\\+");
        rawDelimiter2Regex.put("?", "\\?");
        rawDelimiter2Regex.put("^", "\\^");
        rawDelimiter2Regex.put("$", "\\$");
        rawDelimiter2Regex.put("#", "\\#");
        rawDelimiter2Regex.put("&", "\\&");
    }

    public static String getRegex(String rawDelimiter) {
        return rawDelimiter2Regex.getOrDefault(rawDelimiter, "\\" + rawDelimiter);
    }

    public static String trim(String value) {
        return Objects.toString(org.apache.commons.lang3.StringUtils.trim(value), "");
    }

    public static boolean contains(String value, String s) {
        return value != null && value.contains(s);
    }

    public static boolean notContains(String value, String s) {
        return !contains(value, s);
    }

    public static boolean startsWith(String value, String s) {
        return value != null && value.startsWith(s);
    }

    public static boolean endsWith(String value, String s) {
        return value != null && value.endsWith(s);
    }

    public static int length(String delimiters) {
        return delimiters == null ? 0 : delimiters.length();
    }

    public static boolean equals(String s1, String s2) {
        return Objects.equals(s1, s2);
    }

    public static boolean equalsIgnoreCase(String s1, String s2) {
        return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
    }

    public static boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }

    public static boolean isNotBlank(String value) {
        return !isBlank(value);
    }
}
